<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
header('Content-type: text/JavaScript');

//
// On the LIVE SERVER this script will ONLY create a minimized scripts.js and return
// an operation complete alert.
//
// On DevServer adding minimize=y will create a minimized scripts.js unless
// debug=y and password is also specified and then scripts.js will be created but not minimized.
// No options will return the collective unminimized file direct with no changes to
// scripts.js.
//

//echo file_get_contents('scripts.closure.js');
//exit(0);


$passcode = 'SuperBaby'; // Required to allow creating unminimized files
$appPath = 'App';

$devserver = FALSE;

if (strtolower($_SERVER['SERVER_NAME']) == 'localhost')
	$devserver = TRUE;
if (strtolower($_SERVER['SERVER_NAME']) == '192.168.1.19')
	$devserver = TRUE;
if (strtolower($_SERVER['SERVER_NAME']) == 'love.playnexus.com')
	$devserver = TRUE;
//$devserver = FALSE;

$mirrorroot = $_SERVER['DOCUMENT_ROOT'] . '/mirror';

function webPath($path,$file) {
	global $mirrorroot;
	if ($path && substr($path,-1,1) != '/') $path .= '/';
	$path = $path . $file;
	if (substr($path,0,1) == '/') $path = $_SERVER['DOCUMENT_ROOT'] . $path;
	if (substr($path,0,2) == ':/') $path = substr($path,1);
	if (substr($path,0,2) == '@/') $path = $mirrorroot . substr($path,1);
	if ($path && substr($path,-1,1) == '/') $path = substr($path,0,strlen($path)-1);
	return $path;
}

function req($nm, $default) {
  try {
	$v = $_POST[$nm];
	if ($v == 'TRUE' || $v == 'true')
		return TRUE;
	if ($v == 'FALSE' || $v == 'false')
		return FALSE;
	if ($v) {
		if (0 === strpos($v, 'transfer/double-encoded:'))
			return pureTx(rawurldecode(substr($v, 24)));
		return pureTx($v);
	}
	$v = $_GET[$nm];
	if ($v == 'TRUE' || $v == 'true')
		return TRUE;
	if ($v == 'FALSE' || $v == 'false')
		return FALSE;
	if ($v)
		return pureTx($v);
	return $default;
  }
  catch (Exception $ex)
  {
    return $default;
  }
}

function pureTx($tx) {
	if (get_magic_quotes_gpc() == 0)
		return $tx;
	return stripslashes($tx);
}

$minimize = req('minimize', FALSE);
$debug = req('debug', FALSE);
$password = req('password', '');
$authenticated = FALSE;

if ($minimize === 'y')
	$minimize = TRUE;
if ($debug === 'y')
	$debug = TRUE;

$includeList = array(
  //'jquery-1.7.1.js',
  'Scripts/Xtra/printStackTrace.js',
  'Scripts/Xtra/Encoder.js',
  'Scripts/Xtra/KeyEvent.js',
  'Scripts/Xtra/lib.json.js',
  'Scripts/Selectors.js',
  'Scripts/Snippits.js',
  'Scripts/SnapTo.js',
  'Scripts/ParseXmlLib.js',
  'Scripts/jquery.textarea-expander.js',
  'Scripts/ImageRotate/jQueryRotate2.1.js',
  'Scripts/mscorlib.debug.js',
  'Scripts/Nsb.debug.js',
  'Scripts/Actors.debug.js',
  'Scripts/Yoshi.debug.js',
  'Scripts/PlayNexus.debug.js',
  'Scripts/MetaPages.debug.js',
  'Scripts/ParseXml.debug.js',
  "../$appPath/sc/Theme.debug.js"
);

$minList = array(
  //'jquery-1.7.1.min.js',
  'Scripts/Xtra/printStackTrace.js',
  'Scripts/Xtra/Encoder.js',
  'Scripts/Xtra/KeyEvent.js',
  'Scripts/Xtra/lib.json.js',
  'Scripts/Selectors.js',
  'Scripts/Snippits.js',
  'Scripts/SnapTo.js',
  //'Scripts/ParseXml.js',
  'Scripts/jquery.textarea-expander.js',
  'Scripts/ImageRotate/jQueryRotate2.1.js',
  'Scripts/mscorlib.js',
  'Scripts/Nsb.js',
  'Scripts/Actors.js',
  'Scripts/Yoshi.js',
  'Scripts/PlayNexus.js',
  'Scripts/ParseXml.js',
  "../$appPath/sc/Theme.js"
);

$content = '';
$msg = '';

// Bump the refresh
file_put_contents("../$appPath/@MasterVersion.txt", date('r'), LOCK_EX);

$content .= "window.DevServer = " . ($devserver ? 'true' : 'false') . ";\n";
$content .= "window.ScriptVersion = " . (time() * 1000) . ";\n";
$content .= "// Created " . date('r') . "\n";
$content .= "// Timezone " . date_default_timezone_get() . "\n";
$content .= "//\n";

if ($password && $passcode && $password === $passcode)
{
	$msg .= "Development authentication passed";
	$authenticated = TRUE;
	//$devserver = TRUE;
}
else
{
	$minimize = TRUE;
	$debug = FALSE;
	if ($password)
		$msg .= "Development authentication failed";
}

if (($minimize === TRUE && $debug === FALSE) || $devserver === FALSE) {
	$includeList = $minList;
}

foreach ($includeList as $name) {
	//if (file_exists($name))
		$content .= file_get_contents($name) . "\n\n";
}

//$content .= "\nMaster.MasterPage();\n";

if ($minimize === TRUE || $devserver === FALSE) {
	if ($debug === TRUE && $authenticated === TRUE && $devserver === TRUE) {
		file_put_contents('Scripts/master.js', $content);
		$minimized = $content;
	} else {
		include_once("master.jsmin.php");
		try {
			$minimized = JSMin::minify($content);
			//$minimized = $content;
			$didMin = 'true';
		} catch (Exception $e) {
			$error = $e->getMessage();
		}
		$cp = '';
		$cp .= "/*\n";
		$cp .= " * File contents and programs Copyright(c) playnexus.com and Nathan S. Bawden 2012 all rights reserved\n";
		$cp .= " */\n";
		file_put_contents('Scripts/master.js', $cp . $minimized);
		//file_put_contents('Scripts/master.debug.js', $content);
	}
	$startSize = strlen($content);
	$endSize = strlen($minimized);
	$percent = 100 * (1 - ($endSize / $startSize));

	if ($msg)
		echo ";\nif (window.console) console.log('$msg');\n";
	if ($devserver === TRUE)
	{
		echo "if (window.console) console.log('Minimized scripts $startSize Bytes to $endSize Bytes ($percent%) ServerName=" . $_SERVER['SERVER_NAME'] . "');";
		echo "if (window.localStorage) localStorage.setItem('LoadLog','Minimized scripts $startSize Bytes to $endSize Bytes ($percent%) ServerName=" . $_SERVER['SERVER_NAME'] . "');";
	}
	else
		echo "if (window.localStorage) localStorage.setItem('LoadLog','Minimized master.js build complete.');";
	//echo $minimized;
	exit(0);
}

if ($devserver === TRUE)
{
	echo $content;
	if ($msg)
		echo ";\nif (window.console) console.log('$msg');\n";
}
else
	echo "alert('invalid operation " . strtolower($_SERVER['SERVER_NAME']) . "');";
?>